package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.TimeTableVO;
import edu.zqu.instructionalMatters.project.entity.TimeTable;
import edu.zqu.instructionalMatters.project.mapper.TimeTableMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 作息表 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "作息")
public class TimeTableController {
    @Autowired
    private TimeTableMapper timeTableMapper;
    @Autowired
    private CollegeService collegeService;

    @ApiOperation(value = "获取作息信息列表")
    @RequestMapping(value = "/timeTables", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "作息id", paramType = "query", required = false),
            @ApiImplicitParam(name = "name", value = "作息名称关键字", paramType = "query", required = false),
            @ApiImplicitParam(name = "englishName", value = "英文名称关键字", paramType = "query", required = false),
            @ApiImplicitParam(name = "period", value = "对应时间", paramType = "query", required = false)

    })
    public ZQUResult get(BigInteger collegeId,
                         BigInteger id,
                         String name,
                         String englishName,
                         String period
    ) {
        TimeTable timeTable1 = new TimeTable()
                .setCollegeId(collegeId).setId(id).setName(name).setEnglishName(englishName).setPeriod(period);         //合成TimeTable对象
        List<TimeTable> list1 = timeTableMapper.findAll(timeTable1);                                                    //获取 TimeTable 集合
        List<TimeTableVO> list = new ArrayList<>();                                                                     //初始化 TimeTableVO 集合
        for (TimeTable timeTable : list1) {                                                                              //循环遍历
            TimeTableVO timeTableVO = new TimeTableVO();                                                                //初始化 TimeTableVO
            BeanUtils.copyProperties(timeTable, timeTableVO);                                                            //对象复制赋值
            String collegeName = collegeService.getCollegeName(timeTable.getCollegeId());                               //获取学校名称
            timeTableVO.setCollegeName(collegeName);                                                                    //设置学校名称
            list.add(timeTableVO);                                                                                      //加入 list
        }
        return ZQUResult.success(list);                                                                                 //返回 list
    }

    @ApiOperation(value = "获取作息信息", response = TimeTableVO.class)
    @RequestMapping(value = "/timeTable/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "作息id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        TimeTable timeTable = timeTableMapper.selectById(id);                                             //查询对象数据
        String collegeName = collegeService.getCollegeName(timeTable.getCollegeId());                     //获取学校名称
        TimeTableVO timeTableVO = new TimeTableVO();                                                      //初始化VO
        BeanUtils.copyProperties(timeTable, timeTableVO);                                                  //对象复制赋值
        timeTableVO.setCollegeName(collegeName);                                                          //设置学校名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", timeTableVO);                           //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/timeTable")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "period", value = "对应时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger schoolId,
                         String name,
                         String englishName,
                         String period,
                         BigInteger byWho) {
        TimeTable timeTable = new TimeTable();                            //初始化
        timeTable.setCollegeId(collegeId);                                //设置学校id
        timeTable.setSchoolId(schoolId);                                  //设置校区id
        timeTable.setName(name);                                          //设置作息名称
        timeTable.setEnglishName(englishName);                            //设置作息英文名称
        timeTable.setPeriod(period);                                      //设置对应时间
        timeTable.setByWho(byWho);                                        //设置操作者
        timeTableMapper.insert(timeTable);                                //执行插入
        return ZQUResult.success();                                       //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/timeTable/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "period", value = "对应时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger schoolId,
                            String name,
                            String englishName,
                            String period,
                            BigInteger byWho) {
        TimeTable timeTable = timeTableMapper.selectById(id);             //先查出旧纪录
        timeTable.setByWho(byWho);                                        //修改操作者
        timeTableMapper.updateById(timeTable);                            //更新操作者
        timeTableMapper.deleteById(id);                                   //逻辑删除
        timeTable = new TimeTable();                                      //初始化新记录
        timeTable.setCollegeId(collegeId);                                //设置学校id
        timeTable.setSchoolId(schoolId);                                  //设置校区id
        timeTable.setName(name);                                          //设置作息名称
        timeTable.setEnglishName(englishName);                            //设置作息英文名称
        timeTable.setPeriod(period);                                      //设置对应时间
        timeTable.setByWho(byWho);                                        //设置操作者
        timeTable.setLastVersion(id);                                     //设置上一版本
        timeTableMapper.insert(timeTable);                                //执行插入
        return ZQUResult.success();                                       //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/timeTable/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        TimeTable timeTable = timeTableMapper.selectById(id);             //先查出旧纪录
        timeTable.setByWho(byWho);                                        //修改操作者
        timeTableMapper.updateById(timeTable);                            //更新操作者
        timeTableMapper.deleteById(id);                                   //逻辑删除
        return ZQUResult.success();                                       //操作成功返回
    }
}

